<table id="loc-setting-table" class="easyui-datagrid" title="机房设置" style="height:auto"></table>
<div id="loc-toolbar" class="mitake-table-toolbar">
  <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="add()">增加</a>
  <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="remove()">删除</a>
  <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="save()">保存</a>
  <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">回复</a>
</div>
<script type="text/javascript">
var editIndex = undefined;

$(document).ready(function()
{
	$('#loc-setting-table').datagrid({
		iconCls: 'icon-edit',
		singleSelect: true,
		toolbar: '#loc-toolbar',
		onClickRow: onClickRow,
		fitColumns: true,
		border: false,
		rownumbers: true,
	    nowrap: true,
	    pagination: true,
	    pagePosition: 'bottom', //、'top','bottom'、'both'。
	    pageNumber: 1,
	    pageSize: 20,
	    pageList: [20, 40, 60, 100, 150, 200],
		columns:
		[[
	  		{field:'a',title:'行情类别',width:150,editor:
	  			{
	  				type:'text'
	  			}
	  		},
	  		{field:'b',title:'服务器IP',width:150,editor:
	  			{
	  				type:'text'
	  			}
	  		},
	  		{field:'c',title:'服务器端口',width:150,editor:
	  			{
	  				type:'numberbox'
	  			}
	  		},
	  		{field:'d',title:'说明',width:200,editor:'text'}
	  	]]
	});
	
	getData();
});

function getData()
{
	send('/a', 'event=g', function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$.messager.alert('通知', errorThrown, 'error');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);
		
		if(info.code == '00000')
		{
			var rowCount = info.host.length;
			
			if(rowCount > 0)
			{
				var jd = '';
				jd += '{\"total\":';
				jd += rowCount + ',';
				jd += '\"rows\":[';
				
				for(i = 0 ; i < rowCount ; i++)
				{
					if(i > 0)
					{
						jd += ',';
					}
					
					jd += '{\"a\":\"' + info.host[i].type + '\",\"b\":\"' + info.host[i].ip + '\",\"c\":\"' + info.host[i].port + '\",\"d\":\"' + info.host[i].desc + '\"}';
				}
				
				jd += ']}';

				$('#loc-setting-table').datagrid('loadData', JSON.parse(jd));
			}
		}
		else
		{
			$.messager.alert('通知', info.message, 'error');
		}
	});
}

function endEditing()
{
	if(editIndex == undefined)
	{
		return true;
	}
	
	if($('#loc-setting-table').datagrid('validateRow', editIndex))
	{
		var ed = $('#loc-setting-table').datagrid('getEditor', {index:editIndex,field:''});
		//var productname = $(ed.target).combobox('getText');
		//$('#loc-setting-table').datagrid('getRows')[editIndex]['productname'] = productname;
		$('#loc-setting-table').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} 
	else 
	{
		return false;
	}
}

function onClickRow(index)
{
	if(editIndex != index)
	{
		if(endEditing())
		{
			$('#loc-setting-table').datagrid('selectRow', index).datagrid('beginEdit', index);
			editIndex = index;
		} 
		else 
		{
			$('#loc-setting-table').datagrid('selectRow', editIndex);
		}
	}
}

function add()
{
	if(endEditing())
	{
		$('#loc-setting-table').datagrid('appendRow',{});
		editIndex = $('#loc-setting-table').datagrid('getRows').length - 1;
		$('#loc-setting-table').datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex);
	}
}

function remove()
{
	if(editIndex == undefined)
	{
		return;
	}
	
	$('#loc-setting-table').datagrid('cancelEdit', editIndex).datagrid('deleteRow', editIndex);
	editIndex = undefined;
}

function save()
{
	if(endEditing())
	{
		$('#loc-setting-table').datagrid('acceptChanges');
	}
}

function reject()
{
	$('#loc-setting-table').datagrid('rejectChanges');
	editIndex = undefined;
}
</script>